from bs4 import BeautifulSoup
import requests

count = 0
for page in range(1, 14):
    if page == 1:
        url = "https://book.douban.com/latest"
    else:
        url = f"https://book.douban.com/latest?subcat=%E5%85%A8%E9%83%A8&p={page}"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
    }

    response = requests.get(url, headers=headers)
    print(response.status_code)
    result = response.content.decode()
    root = BeautifulSoup(result, "lxml")
    information_list = root.select("li[class='media clearfix']")
    for information in information_list:
        title = information.select("div[class='media__body']>h2[class='clearfix']>a")[0].text
        detail = information.select('div[class="media__body"]>p[class="subject-abstract color-gray"]')[0].text
        try:
            price = information.select(
                'div[class="media__body"]>div[class="clearfix w250 color-gray fleft"]>span[class="buy-info"]>a')[0].text
        except:
            price = "暂无价格"
        detail = detail.strip()
        price = price.strip()
        count +=1
        print(count,title, detail, price)
    last_book_title = root.select("li[class='media clearfix last']")[0].select("div[class='media__body']>h2[class='clearfix']>a")[0].text
    last_book_detail = root.select("li[class='media clearfix last']")[0].select('div[class="media__body"]>p[class="subject-abstract color-gray"]')[0].text
    try:
        last_book_price = root.select("li[class='media clearfix last']")[0].select('div[class="media__body"]>div[class="clearfix w250 color-gray fleft"]>span[class="buy-info"]>a')[0].text
    except:
        last_book_price = "暂无价格"
    count+=1
    last_book_title = last_book_title.strip()
    last_book_detail = last_book_detail.strip()
    last_book_price = last_book_price.strip()
    print(count,last_book_title, last_book_detail, last_book_price)